
package com.yimixian.app.order;

import com.yimixian.app.R;
import com.yimixian.app.model.KV;
import com.yimixian.app.model.ValidatedOrder;
import com.yimixian.app.ui.dialog.WheelViewDialog;
import com.yimixian.app.util.CustomViewUtils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.List;

public class DeliveryModelNewView extends LinearLayout {
    @InjectView(R.id.delivery_text)
    TextView mDeliveryText;
    List<KV> DeliveryMap = new ArrayList<>();
    KV mSelectKV = new KV();
    WheelViewDialog wheelViewDialog;

    public DeliveryModelNewView(Context context) {
        super(context);
        initDeliveryModeView();
    }

    public DeliveryModelNewView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDeliveryModeView();
    }

    public DeliveryModelNewView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDeliveryModeView();
    }

    private void initDeliveryModeView() {
        LayoutInflater.from(getContext()).inflate(R.layout.delivery_mode_view, this);
        ButterKnife.inject(this);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
        showDeliver();
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wheelViewDialog != null && !wheelViewDialog.isShowing())
                    wheelViewDialog.show();
            }
        });
    }

    public void bind(List<ValidatedOrder.DeliveryOptions> deliveryOptionsList, int deliveryId,
            final OnClickListener onClickListener, Activity activity) {
        DeliveryMap.clear();
        for (int i = 0; i < deliveryOptionsList.size(); i++) {
            ValidatedOrder.DeliveryOptions deliveryOptions = deliveryOptionsList.get(i);
            KV Options = new KV();
            Options.key = deliveryOptions.getId() + "";
            Options.value = deliveryOptions.getName();
            DeliveryMap.add(Options);
            if (deliveryId == deliveryOptions.getId()) {
                mSelectKV.value = deliveryOptions.getName();
                mSelectKV.key = deliveryOptions.getId() + "";
                showDeliver();
            }
        }
        wheelViewDialog = new WheelViewDialog(activity,
                DeliveryMap, mSelectKV);
        wheelViewDialog
                .setmClickSureListener(new WheelViewDialog.ClickSureListener() {
                    @Override
                    public void onConfirm(KV kv) {
                        mSelectKV.key = kv.key;
                        mSelectKV.value = kv.value;
                        showDeliver();
                        onClickListener.onClick(DeliveryModelNewView.this);
                    }

                    @Override
                    public void onTwoSelectConfirm(String province, String city) {

                    }
                });
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setEnabled(enabled);
        }
    }

    public void showDeliver() {
        if (!TextUtils.isEmpty(mSelectKV.value)) {
            mDeliveryText.setText(mSelectKV.value);
        } else {
            mDeliveryText.setText("请选择您的收货时间");
        }
    }

    public int getDeliveryModeId() {
        if (!TextUtils.isEmpty(mSelectKV.key)) {
            return Integer.parseInt(mSelectKV.key);
        }
        return -1;
    }

}
